package br.ykhan.t5.ytools.menu.services;

import java.util.Collection;
import java.util.HashSet;

import org.apache.tapestry5.ioc.annotations.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class IgnoredPathsForMenuImpl implements IgnoredPathsForMenu
{
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Inject private PathPatternsMatcher pathPatternsMatcher;
	
	private Collection<String> urlPatterns2Ignore;
	
	public IgnoredPathsForMenuImpl(Collection<String> configuration, 
			PathPatternsMatcher pathPatternsMatcher)
	{
		this.pathPatternsMatcher = pathPatternsMatcher;
		this.urlPatterns2Ignore = new HashSet<String>();
		
		for(String s : configuration)
			if(isValidPattern(s))
				this.urlPatterns2Ignore.add(s);
			else
				logger.debug("Invalid Path specified : "+s);
	}
	
	public boolean isIgnoredPath(String path)
	{
		for(String urlPattern : urlPatterns2Ignore)
			if(pathPatternsMatcher.matches(urlPattern, path))
				return true;
		
		return false;
	}

	public boolean isValidPattern(String pattern)
	{
		return pathPatternsMatcher.isValidPattern(pattern);
	}
}
